<?php

/**
 * XHTML_Form, xhtml_lib
 * 
 * This file contains the source code for the XHTML_Form class.
 * @author Fernando Jimenez Mocobono <fjmcool@gmail.com>
 * @version 0.1
 * @package xhtml_lib
 */

include_once "XHTML_Common_Element.php";
include_once "XHTML_Block.php";
include_once "XHTML_Flow.php";
include_once "XHTML__Form_Content.php";


/**
 * The XHTML_Form class is a handler for the form xhtml-strict tag as a part
 * of a xhtml-strict php-handler implementation.
 */
class XHTML_Form extends XHTML_Common_Element implements XHTML_Block, XHTML_Flow {
    
    // xhtml attributes
    
    /**
     * URI that defines where to send the data when the submit button is 
     * pushed.
     * 
     * @var $string
     */
    protected $action;
    
    /**
     * HTTP method for sending data to the action URL.
     * Valid values: get, post.
     *
     * @var string
     */
    protected $method;
    
    /**
     * MIME type used to encode the content of the form.
     *
     * @var string
     */
    protected $encType;
    
    /**
     * Script for the onsubmit event.
     *
     * @var string
     */
    protected $onSubmitScript;
    
    /**
     * Script for the onreset event.
     * 
     * @var string
     */
    protected $onResetScript;
    
    /**
     * A comma separated list of content types that the server that processes 
     * this form will handle correctly.
     *
     * @var string
     */
    protected $accept;
    
    /**
     * A comma separated list of possible character sets for the form data. 
     * The default value is "unknown".
     *
     * @var string
     */
    protected $acceptCharSet;
    
    // inner elements
    
    /**
     * XHTML__Form_Content elements the form element has in its content.
     *
     * @var array
     */
    protected $elements;
    
    
    // Methods
    
    /**
     * Constructs a XHTML_Form object with its required action attribute.
     * 
     * @param string $action
     */
    function __construct($action)
    {
        $this->action = $action;
        settype($this->elements, "array");
    }
    
    /**
     * Sets the URI that defines where to send the data when the submit button 
     * is pushed.
     * 
     * @param string $action
     */
    function setAction($action)
    {
        $this->action = $action;
    }
    
    /**
     * Gets the URI that defines where to send the data when the submit button 
     * is pushed.
     * 
     * @return string
     */
    function getAction()
    {
        return $this->action;
    }
    
    /**
     * Sets the HTTP method for sending data to the action URL.
     * Valid values: get, post.
     *
     * @param string $method
     */
    function setMethod($method)
    {
        $this->method = $method;
    }
    
    /**
     * Gets the HTTP method for sending data to the action URL.
     * Valid values: get, post.
     *
     * @return string
     */
    function getMethod()
    {
        return $this->method;
    }
    
    /**
     * Sets the MIME type used to encode the content of the form.
     * 
     * @param string $encType
     */
    function setEncType($encType)
    {
        $this->encType = $encType;
    }
    
    /**
     * Gets the MIME type used to encode the content of the form.
     *
     * @return string
     */
    function getEncType()
    {
        return $this->encType;
    }
    
    /**
     * Sets the script for the onsubmit event.
     *
     * @param string $script
     */
    function setOnSubmit($script)
    {
        $this->onSubmitScript = $script;
    }
    
    /**
     * Gets the script for the onsubmit event.
     * 
     * @return string
     */
    function getOnSubmit()
    {
        return $this->onSubmitScript;
    }
    
    /**
     * Sets the script for the onreset event.
     * 
     * @param string $script
     */
    function setOnReset($script)
    {
        $this->onResetScript = $script;
    }
    
    /**
     * Gets the script for the onreset event.
     * 
     * @return string
     */
    function getOnReset()
    {
        return $this->onResetScript;
    }
    
    /**
     * Sets a comma separated list of content types that the server that 
     * processes this form will handle correctly.
     * 
     * @param string $accept
     */
    function setAccept($accept)
    {
        $this->accept = $accept;
    }
    
    /**
     * Gets a comma separated list of content types that the server that 
     * processes this form will handle correctly.
     *
     * @return string
     */
    function getAccept()
    {
        return $this->accept;
    }
    
    /**
     * Sets a comma separated list of possible character sets for the form data. 
     * The default value is "unknown".
     * 
     * @package string $acceptCharSet
     */
    function setAcceptCharSet($acceptCharSet)
    {
        $this->acceptCharSet = $acceptCharSet;
    }
    
    /**
     * Gets a comma separated list of possible character sets for the form data.
     * 
     * @return string
     */
    function getAcceptCharSet()
    {
        return $this->acceptCharSet;
    }
 
    /**
     * Sets the XHTML__Form_Content elements array.
     *
     * @param array
     */
    function setElements($elements) {
        $this->elements = $elements;
    }
    
    /**
     * Gets the XHTML__Form_Content elements array.
     *
     * @return array
     */
    function getElements()
    {
        return $this->elements;
    }
    
    /**
     * Adds a XHTML__Form_Content element to the inner array of this object.
     * 
     * @param XHTML__Form_Content
     */
    function addElement(XHTML__Form_Content $element)
    {
    	$this->elements[] = $element;
    }
    
    /**
     * Removes a XHTML__Form_Content element from the inner array of this object.
     * 
     * @param XHTML__Form_Content
     */
    function removeElement(XHTML__Form_Content $element)
    {
        if (($res = array_search($element, $this->elements)) !== false) {
            unset($this->element[$res]);
        }
    }

    // Overriding the toString method for a proper implementation.
    
    /**
    * Returns the string representation for echoing the class atributes.
    *
    * @return string
    */
    function toString()
    {
        $auxString = "<form".parent::toString();
        
        /*
         * Additions of the object own attributes to the return string.
         */
        
        $auxString = $auxString."action=\"".$this->action."\" ";
        
        if (isset($this->method)) {
            $auxString = $auxString."method=\"".$this->method."\" ";
        }
        
        if (isset($this->encType)) {
            $auxString = $auxString."enctype=\"".$this->encType."\" ";
        }
        
        if (isset($this->onSubmitScript)) {
            $auxString = $auxString."onsubmit=\"".$this->onSubmitScript."\" ";
        }
        
        if (isset($this->onResetScript)) {
            $auxString = $auxString."onreset=\"".$this->onResetScript."\" ";
        }
        
        if (isset($this->accept)) {
            $auxString = $auxString."accept=\"".$this->accept."\" ";
        }
        
        if (isset($this->acceptCharSet)) {
            $auxString = $auxString."accept-charset=\"".$this->acceptCharSet."\" ";
        }
        
        $auxString = $auxString.">\n";
        
        /*
         * Additions of the inner elements of the form element to the return 
         * string.
         */
        if (count($this->elements) > 0) {
            foreach ($this->elements as $element) {
                $auxString = $auxString.$element->toString();
            }
        }
        
        $auxString = $auxString."</form>\n";
        
        return $auxString;
    }
    
}

?>